import { useDataStore, useSnippetsStore } from '@renderer/stores'

export function useSearchBookmarks () {
  const bookmarks = useDataStore(s => s.bookmarks)
  const setSnippets = useSnippetsStore(state => state.setSnippets)

  const searchBookmarks = (keyword: string) => {
    const filtered = bookmarks.filter(bookmark => {
      return bookmark.name.toLowerCase().includes(keyword)
    })

    setSnippets(
      filtered.map(bookmark => {
        return {
          id: bookmark.id,
          title: '书签搜索',
          content: bookmark.name,
          category_id: 0,
          created_at: bookmark.date_added,
          url: bookmark.url,
        }
      }),
    )
  }

  return {
    searchBookmarks,
  }
}
